<?php
class JiFenService
{
	/**
	 * 查询列表
	 */
	public function getListBySql($sql)
	{
		$list = array();
		$query = DB::query($sql);
		while($data = DB::fetch($query))
		{
			if(!parseUrl($data['small_img']))
				$data['small_img'] = SITE_PATH.$data['small_img'];
				
			$data['exchange_count']  = intval($data['exchange_count']);
			
			if($data['exchange_count'] >= $data['stock'])
				$data['is_null']  = true;
				
			$data['end_time_format']  = toDate($data['end_time']);
			$data['site_url'] = U("site/show",array("id"=>$data['site_id']));
			$data['goods_url'] = U("goods/show",array("id"=>$data['goods_id']));
			$list[] = $data;
		}
		
		return $list;
	}
	
	/**
	 * 推荐列表
	*/
	public function getBestList()
	{
		$sql = 'SELECT eg.*,g.name as goods_name,g.small_img,g.city,g.site_name,g.site_id,sum(o.data_num) as exchange_count 
				FROM '.DB::table('exchange_goods').' AS eg 
				INNER JOIN '.DB::table('goods').' AS g ON g.id = eg.goods_id 
				LEFT JOIN '.DB::table('order').' AS o ON o.rec_id = eg.id 
				WHERE eg.status = 1 AND eg.is_best = 1 AND (eg.begin_time <= '.TIME_UTC.' OR eg.begin_time = 0) 
				AND (eg.end_time >= '.TIME_UTC.' OR eg.end_time = 0) 
				GROUP BY eg.id ORDER BY eg.sort ASC,eg.end_time ASC,eg.id DESC';
				
		return $this->getListBySql($sql);
	}
	
	public function getItem($id)
	{
		return DB::fetchFirst('SELECT eg.*,g.name as goods_name,sum(o.data_num) AS exchange_count 
				FROM '.DB::table('exchange_goods').' AS eg 
				INNER JOIN '.DB::table('goods').' AS g ON g.id = eg.goods_id 
				LEFT JOIN '.DB::table('order').' AS o ON o.rec_id = eg.id 
				WHERE eg.id = '.$id.' AND eg.status = 1 AND (eg.begin_time <= '.TIME_UTC.' OR eg.begin_time = 0) 
				AND (eg.end_time >= '.TIME_UTC.' OR eg.end_time = 0) 
				GROUP BY eg.id');
	}
	
	/**
	 * 兑换专区
	*/
	public function getList()
	{
		$sql = 'SELECT eg.*,g.name as goods_name,g.small_img,g.city,g.site_name,g.site_id,sum(o.data_num) as exchange_count 
				FROM '.DB::table('exchange_goods').' AS eg 
				INNER JOIN '.DB::table('goods').' AS g ON g.id = eg.goods_id 
				LEFT JOIN '.DB::table('order').' AS o ON o.rec_id = eg.id 
				WHERE eg.status = 1 AND eg.is_best = 0 AND (eg.begin_time <= '.TIME_UTC.' OR eg.begin_time = 0) 
				AND (eg.end_time >= '.TIME_UTC.' OR eg.end_time = 0) 
				GROUP BY eg.id ORDER BY eg.sort ASC,eg.end_time ASC,eg.id DESC';
		
		return $this->getListBySql($sql);
	}
	
	/**
	 * 最新兑换
	*/
	public function getOrderTop($num = 5)
	{
		$sql = 'SELECT o.data_name,o.user_name,o.create_time,o.data_num,g.site_name 
				FROM '.DB::table('order').' AS o 
				INNER JOIN '.DB::table('exchange_goods').' AS eg ON eg.id = o.rec_id 
				INNER JOIN '.DB::table('goods').' AS g ON g.id = eg.goods_id 
				ORDER BY o.id DESC LIMIT 0,'.$num;
		
		$list = array();
		$query = DB::query($sql);
		while($data = DB::fetch($query))
		{
			$data['create_time_format']  = toDate($data['create_time'],'H:i:s');
			$list[] = $data;
		}
		
		return $list;
	}
	
	/**
	 * 积分排行
	*/
	public function getScoreTop($num = 10)
	{
		$sql = 'SELECT user_name,score FROM '.DB::table('user').' 
				WHERE status = 1 ORDER BY score DESC LIMIT 0,'.$num;
		
		$list = array();
		$query = DB::query($sql);
		while($data = DB::fetch($query))
		{
			$list[] = $data;
		}
		
		return $list;
	}
	
	/**
	 * 兑换排行
	*/
	public function getExchangeTop($num = 10)
	{
		$sql = "SELECT o.user_name,SUM(o.data_num) AS sum_count ".
			    'FROM '.DB::table('order').' AS o '.
				"GROUP BY o.user_id ORDER BY sum_count DESC LIMIT 0,$num";
		
		$list = array();
		$query = DB::query($sql);
		while($data = DB::fetch($query))
		{
			$list[] = $data;
		}
		
		return $list;
	}
	
	/**
	 * 获取积分说明
	*/
	public function getExchangeSetting()
	{
		$sql = 'SELECT * FROM '.DB::table('exchange_settings');
		
		$list = array();
		$query = DB::query($sql);
		while($data = DB::fetch($query))
		{
			$list[$data['code']] = $data['val'];
		}

		return $list;
	}
}
?>